<?php

class AdminMiscellaneousController extends AdminAppController {
	
	public $name = 'Miscellaneous';
	
	public $uses = array('Pet', 'Product', 'Story', 'Animal');
	
	public function animals() {
		$this->Animal->recursive = 0;
		$this->paginate = array (
			'Animal' => array(
				'order' => array('Animal.id' => 'desc')
			)
		);
		$this->set('animals', $this->paginate('Animal'));
	}
	
	public function create_animal() {
		if( $this->request->is('post') ) {
			$this->Animal->create();
			if( $this->Animal->save($this->request->data) ) {
				$this->setUserInfo(__d('admin', 'The Animal has been created.'), 'success');
				$this->redirect(array('action' => 'animals'));
			}
			else {
				$this->log('Error creating Breed: ' . print_r($this->Animal->validationErrors, true));
				$this->setUserInfo(__d('admin', 'The Animal could not be saved. Please, try again.'), 'error');
			}
		}
	}
	
	public function breeds() {
		
	}
	
	public function create_breed() {
		if( $this->request->is('post') ) {
			$this->Pet->Breed->create();
			if( $this->Pet->Breed->save($this->request->data) ) {
				$this->setUserInfo(__d('admin', 'The Breed has been created.'), 'success');
				$this->redirect(array('action' => 'breed'));
			}
			else {
				$this->log('Error creating Breed: ' . print_r($this->Pet->Breed->validationErrors, true));
				$this->setUserInfo(__d('admin', 'The Breed could not be saved. Please, try again.'), 'error');
			}
		}
	}
	
	public function create_story_category() {
		if( $this->request->is('ajax') ) {
			
			if( $this->Story->Category->save( $this->request->data ) ) {
				$this->Story->Category->recursive = 0;
				$category = $this->Story->Category->read();
				$result = array(
					'status' => 1,
					'result' => $this->Story->Category->generateTreeList(null, null, null, "&nbsp;&nbsp;&nbsp;"),
					'category' => $category['Category']
				);
			}
			else {
				$result = array(
					'status' => 0,
					'error' => $this->Story->Category->validationErrors,
					'result' => $this->Story->Category->generateTreeList(null, null, null, "&nbsp;&nbsp;&nbsp;")
				);
			}
			
			$this->header("Content-type: application/json");
			$this->autoRender = false;
			$this->autoLayout = false;
			echo json_encode( $result );
		}
	}
	
	public function create_product_category() {
		if( $this->request->is('ajax') ) {
			
			if( $this->Product->Category->save( $this->request->data ) ) {
				$this->Product->Category->recursive = 0;
				$category = $this->Product->Category->read();
				$result = array(
					'status' => 1,
					'result' => $this->Product->Category->generateTreeList(null, null, null, "&nbsp;&nbsp;&nbsp;"),
					'category' => $category['Category']
				);
			}
			else {
				$result = array(
					'status' => 0,
					'error' => $this->Product->Category->validationErrors,
					'result' => $this->Product->Category->generateTreeList(null, null, null, "&nbsp;&nbsp;&nbsp;")
				);
			}
			
			$this->header("Content-type: application/json");
			$this->autoRender = false;
			$this->autoLayout = false;
			echo json_encode( $result );
		}
	}
	
}
